
treat_dates <- analysis_df %>%
  group_by(city, YEAR, occ_recode, treat, all, police, fire) %>% 
  summarise() %>%
  left_join(analysis_df %>%
              group_by(city, YEAR) %>%
              summarise(govt_n_total = sum(num[industry_recode=='local']))) %>%
  rename(occ = occ_recode)

#race x nativity

race = prop.wide(analysis_df, var1 = "race_recode", var2 = "birthplace_recode_first") %>%
  mutate_at(vars(black_x_foreign_born:white_x_native_born), funs(ifelse(is.na(.), 0, .)))

race_govt = prop.wide(analysis_df, var1 = "race_recode", var2 = "birthplace_recode_first", subset = "industry_recode=='local'") %>%
  mutate_at(vars(black_x_foreign_born:white_x_native_born), funs(ifelse(is.na(.), 0, .))) %>%
  rename_at(vars(n_total:white_x_native_born), funs(paste0("govt_", .)))

analysis_df_1 = left_join(race, race_govt) %>%
  mutate_at(vars(n_total:white_x_native_born), funs(ifelse(is.na(.), 0, .)))


#main analysis - foreign born grouping

race = prop.wide(analysis_df, var1 = "birthplace_recode_foreign") %>%
  mutate_at(vars(n_total:russian), funs(ifelse(is.na(.), 0, .)))

race_govt = prop.wide(analysis_df, var1 = "birthplace_recode_foreign", subset = "industry_recode=='local'") %>%
  mutate_at(vars(n_total:russian), funs(ifelse(is.na(.), 0, .))) %>%
  rename_at(vars(n_total:russian), funs(paste0("govt_", .)))

analysis_df_2 = left_join(race, race_govt) %>%
  mutate_at(vars(n_total:govt_russian), funs(ifelse(is.na(.), 0, .))) %>%
   left_join(treat_dates %>%
               filter(occ=='blue_collar') %>%
               group_by(city, treat, all, police, fire) %>%
               summarise())

#main analysis - foreign born grouping with type of work

race = prop.wide(analysis_df, var1 = "race_recode", var2 = "birthplace_recode_first") %>%
  mutate_at(vars(black_x_foreign_born:white_x_native_born), funs(ifelse(is.na(.), 0, .)))

race_govt = 
  prop.wide.job.stack(analysis_df, var1 = "race_recode", var2 = "birthplace_recode_first", subset = "industry_recode=='local'") %>%
  mutate_at(vars(black_x_foreign_born:white_x_native_born), funs(ifelse(is.na(.), 0, .))) %>%
  rename_at(vars(black_x_foreign_born:white_x_native_born), funs(paste0("govt_", .)))

analysis_df_3 = full_join(race_govt, race) %>%
  left_join(treat_dates)

####FOREIGN BREAKDOWN

race = prop.wide(analysis_df, var1 = "birthplace_recode_foreign") %>%
  mutate_at(vars(german:russian), funs(ifelse(is.na(.), 0, .)))

race_govt = 
  prop.wide.job.stack(analysis_df, var1 = "birthplace_recode_foreign", subset = "industry_recode=='local'") %>%
  mutate_at(vars(german:russian), funs(ifelse(is.na(.), 0, .))) %>%
  rename_at(vars(german:russian), funs(paste0("govt_", .)))

analysis_df_4 = left_join(race_govt, race) %>%
  left_join(treat_dates)

##LITERACY BREAKDOWN -- NATIVITY

race = prop.wide(analysis_df, var1 = "race_recode", var2 = "birthplace_recode_first", var3 = "literacy_recode") %>%
  mutate_at(vars(black_x_foreign_born_x_literate:white_x_native_born_x_nonliterate), funs(ifelse(is.na(.), 0, .)))


race_govt = 
  prop.wide.job.stack(analysis_df, var1 = "race_recode", var2 = "birthplace_recode_first", var3 = "literacy_recode",
            subset = "industry_recode=='local'") %>%
  mutate_at(vars(black_x_foreign_born_x_literate:white_x_native_born_x_nonliterate), funs(ifelse(is.na(.), 0, .))) %>%
  rename_at(vars(black_x_foreign_born_x_literate:white_x_native_born_x_nonliterate), funs(paste0("govt_", .)))

analysis_df_5 = left_join(race_govt, race) %>%
  left_join(treat_dates)


##LITERACY BREAKDOWN -- FOREIGN

race = prop.wide(analysis_df, var1 = "birthplace_recode_foreign", var2 = "literacy_recode") %>%
  mutate_at(vars(german_x_literate:russian_x_nonliterate), funs(ifelse(is.na(.), 0, .)))


race_govt = 
  prop.wide.job.stack(analysis_df, var1 = "birthplace_recode_foreign", var2 = "literacy_recode",
          subset = "industry_recode=='local'") %>%
  mutate_at(vars(german_x_literate:russian_x_nonliterate), funs(ifelse(is.na(.), 0, .))) %>%
  rename_at(vars(german_x_literate:russian_x_nonliterate), funs(paste0("govt_", .)))

analysis_df_6 = left_join(race_govt, race) %>%
  left_join(treat_dates)


rm(race, race_govt)

